//
//  BSKMainViewController.m
//  AlarmClock
//
//  Created by aaaa on 2016/10/15.
//  Copyright © 2016年 apple. All rights reserved.
//


#import "BSKMainViewController.h"
#import "BSKConsts.h"
#import "SDAutoLayout.h"
#import "BSKStopWatchViewController.h"
#import "BSKTimeKeepingViewController.h"
#import "AlarmClockViewController.h"
#import "BSKCancelAlarmClockViewController.h"
#import "BSKSington.h"
#import "UILocalNotification+setAlarmclock.h"

@interface  BSKMainViewController()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scrollView;
@property (nonatomic,strong) UIView *roundView;
@property (nonatomic,strong) UIButton *alarmClockButton;
@property (nonatomic,strong) UIButton *timeButton;
@property (nonatomic,strong) UIButton *secondButton;
@property (nonatomic,assign) NSInteger curentPage;
@property (nonatomic,strong) UIImageView * bgImage;
@property (nonatomic,assign) BOOL showTimeKeepingAlertFlag;
@end

@implementation BSKMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAperence];
}
-(void)viewDidAppear:(BOOL)animated{
    for(UILocalNotification * notification in [UIApplication sharedApplication].scheduledLocalNotifications){
        if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"StopWatchNoti"]) {
            [self.scrollView setContentOffset:CGPointMake(ScreenWidth, 0) animated:YES];
            break;
        }
    }
    for (UIViewController * vc in self.childViewControllers) {
        if ([vc isKindOfClass:BSKStopWatchViewController.self]) {
            [vc viewDidAppear:animated];
        }
    }
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"neverShowSetingInfoAlert"]) {
        UIAlertController * alertContro = [UIAlertController alertControllerWithTitle:@"请注意！" message:@"由于【iOS系统限制】，使用闹钟时请关闭静音模式，前往【设置】→【快起床】→【通知】在最下方将提醒样式设置为【提醒】来获得更久的响铃持续时间。否则闹钟可能会响铃时间过短或者不响。" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * OKAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDestructive handler:nil];
        UIAlertAction * iKnowAction = [UIAlertAction actionWithTitle:@"不再显示" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"neverShowSetingInfoAlert"];
        }];
        [alertContro addAction:iKnowAction];
        [alertContro addAction:OKAction];
        [self presentViewController:alertContro animated:YES completion:nil];
    }
}

#pragma mark - method
- (void)initializeAperence {
    
    UIImage * bg=[UIImage imageNamed:@"bg.jpg"];
    
    self.bgImage = [[UIImageView alloc]initWithImage:bg];
    self.bgImage.frame = CGRectMake(0, 0, bg.size.width/bg.size.height*ScreenHeight, ScreenHeight);
    [self.view addSubview:self.bgImage];
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.alarmClockButton];
    [self.view addSubview:self.timeButton];
    [self.view addSubview:self.secondButton];
    [self.view addSubview:self.roundView];
    
    self.view.backgroundColor = [UIColor darkGrayColor];
    
    self.scrollView.sd_layout
    .topSpaceToView(self.view,80)
    .leftSpaceToView(self.view,0)
    .rightSpaceToView(self.view,0)
    .bottomSpaceToView(self.view,0);
    self.scrollView.bounces = NO;
    self.alarmClockButton.sd_layout
    .leftSpaceToView(self.view,0)
    .topSpaceToView(self.view,StatusBarHeight)
    .bottomSpaceToView(self.scrollView,0)
    .widthRatioToView(self.view,1.0/3.0);
    
    self.timeButton.sd_layout
    .leftSpaceToView(self.alarmClockButton,0)
    .topSpaceToView(self.view,StatusBarHeight)
    .bottomSpaceToView(self.scrollView,0)
    .widthRatioToView(self.alarmClockButton,1);
    
    self.secondButton.sd_layout
    .leftSpaceToView(self.timeButton,0)
    .topSpaceToView(self.view,StatusBarHeight)
    .bottomSpaceToView(self.scrollView,0)
    .widthRatioToView(self.timeButton,1);
    
    [self.view setNeedsLayout];
    
    self.roundView.center = CGPointMake(ScreenWidth/6, 70);
    
    self.scrollView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.3];
    // 滚动视图
    AlarmClockViewController * alarmClockViewController = [[AlarmClockViewController alloc]init];
    BSKTimeKeepingViewController * timeKeeoingViewController = [[BSKTimeKeepingViewController alloc]init];
    BSKStopWatchViewController * stopWatchViewCOntroller = [[BSKStopWatchViewController alloc]init];
    
    [self addChildViewController:alarmClockViewController];
    [self addChildViewController:timeKeeoingViewController];
    [self addChildViewController:stopWatchViewCOntroller];
    
    CGRect tempframe = self.scrollView.bounds;
    alarmClockViewController.view.frame =tempframe;
    tempframe.origin.x = ScreenWidth;
    stopWatchViewCOntroller.view.frame = tempframe;
    tempframe.origin.x = 2*ScreenWidth;
    timeKeeoingViewController.view.frame = tempframe;
    
    [self.scrollView addSubview:alarmClockViewController.view];
    [self.scrollView addSubview:timeKeeoingViewController.view];
    [self.scrollView addSubview:stopWatchViewCOntroller.view];
    
    
}

#pragma mrk - action
- (void)buttonPressed:(UIButton *)sender {
    NSInteger index = sender.tag - 200;
    
    [self.scrollView setContentOffset:CGPointMake(ScreenWidth*index, 0) animated:YES];
    
    switch (index) {
        case 0:
            break;
        case 1:
            
            break;
        case 2:
            
            break;
            
        default:
            break;
    }
}

#pragma marck UIScroViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGRect f = self.bgImage.frame;
    f.origin.x = -(scrollView.contentOffset.x/(ScreenWidth*2))*(f.size.width-ScreenWidth);
    self.bgImage.frame =f;
    
    CGFloat x = (scrollView.contentOffset.x/ScreenWidth * ScreenWidth/3.0 ) + ScreenWidth/6;
    self.roundView.center = CGPointMake(x, 70);
    NSInteger page = (NSInteger)((scrollView.contentOffset.x+ScreenWidth/2)/ScreenWidth);
    
    if(page == 2 && _curentPage != 2){
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"neverShowTimeKeepingAlert"]) {
            UIAlertController * alertContro = [UIAlertController alertControllerWithTitle:@"请注意！" message:@"由于【iOS系统后台限制】，建议在使用秒表时不要锁定屏幕以及将程序置于后台，这会导致时间不准确。" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * OKAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDestructive handler:nil];
            UIAlertAction * iKnowAction = [UIAlertAction actionWithTitle:@"不再显示" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"neverShowTimeKeepingAlert"];
            }];
            [alertContro addAction:iKnowAction];
            [alertContro addAction:OKAction];
            [self presentViewController:alertContro animated:YES completion:nil];
        }
        
        
    }
    _curentPage = page;
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
}

#pragma mark - getter
- (UIScrollView *)scrollView {
    if (_scrollView) {
        return _scrollView;
    }
    _scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(ScreenWidth*3, ScreenHeight-80);
    _scrollView.delegate = self;
    return _scrollView;
}

- (UIButton *)alarmClockButton {
    if (_alarmClockButton) {
        return _alarmClockButton;
    }
    _alarmClockButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_alarmClockButton setTitle:@"闹钟" forState:UIControlStateNormal];
    _alarmClockButton.tag = 200;
    [_alarmClockButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_alarmClockButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    return _alarmClockButton;
}

- (UIButton *)timeButton {
    if (_timeButton) {
        return _timeButton;
    }
    _timeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_timeButton setTitle:@"计时" forState:UIControlStateNormal];
    _timeButton.tag = 201;
    [_timeButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_timeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    return _timeButton;
}

- (UIButton *)secondButton {
    if (_secondButton) {
        return _secondButton;
    }
    _secondButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_secondButton setTitle:@"秒表" forState:UIControlStateNormal];
    _secondButton.tag = 202;
    [_secondButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_secondButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    return _secondButton;
}

- (UIView *)roundView {
    if (_roundView) {
        return _roundView;
    }
    _roundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,10, 10)];
    _roundView.layer.masksToBounds = YES;
    _roundView.layer.cornerRadius = 5;
    _roundView.backgroundColor = [UIColor whiteColor];
    return _roundView;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end

